package com.atguigu.gmall.product.controller;


import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.model.product.BaseSaleAttr;
import com.atguigu.gmall.model.product.SpuImage;
import com.atguigu.gmall.model.product.SpuInfo;
import com.atguigu.gmall.model.product.SpuSaleAttr;
import com.atguigu.gmall.product.service.ManageService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "spu控制器")
@RestController
@RequestMapping("admin/product")
public class SpuManageController {
    @Autowired
    private ManageService manageService;

    @ApiOperation(value = "获取spu列表")
    @GetMapping("{page}/{limit}")
    public Result getSpuInfoList(@PathVariable Long page, @PathVariable Long limit,
                                 SpuInfo spuInfo){
        Page<SpuInfo> spuInfoPage = new Page<>(page, limit);
        IPage<SpuInfo> iPage = manageService.getSpuInfoList(spuInfoPage,spuInfo);
        return Result.ok(iPage);
    }

    @ApiOperation(value = "获取spu销售属性列表集合")
    @GetMapping("baseSaleAttrList")
    public Result getBaseSaleAttrList(){
        List<BaseSaleAttr> baseSaleAttrList = manageService.getBaseSaleAttrList();
        return Result.ok(baseSaleAttrList);
    }

    @ApiOperation(value = "添加spu信息")
    @PostMapping("saveSpuInfo")
    public Result saveSpuInfo(@RequestBody SpuInfo spuInfo){
        manageService.saveSpuInfo(spuInfo);
        return Result.ok();
    }

    //http://localhost/admin/product/spuImageList/25
    @ApiOperation(value = "根据spuId获取商品图片集合，用于添加sku时的回显")
    @GetMapping("spuImageList/{spuId}")
    public Result getSpuImageListBySpuId(@PathVariable Long spuId){
        List<SpuImage> spuImageList = manageService.getSpuImageListBySpuId(spuId);
        return Result.ok(spuImageList);
    }

    //http://localhost/admin/product/spuSaleAttrList/25
    @ApiOperation(value = "根据spuId查询spu销售属性及值集合")
    @GetMapping("spuSaleAttrList/{spuId}")
    public Result getSpuSaleAttrListBySpuId(@PathVariable Long spuId){
        List<SpuSaleAttr> spuSaleAttrList = manageService.getSpuSaleAttrListBySpuId(spuId);
        return Result.ok(spuSaleAttrList);
    }

}
